/**
 * Created by meiguizhinian on 2018/7/23
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//TODO：等待完成
if (cc.sys.isNative) {

} else {
	let p = ccui.RichText.prototype;

	p.removeAllElement = function () {
		this._richElements = [];
		this._formatTextDirty = true;
	};

	p.getRealSize = function() {
		return this.realSize_;
	};
	p.getRenderElementsRowCount = function () {
		return this._ignoreSize? 1:this._elementRenders.length;
	};

	p.realSize_ = null;//cocos2d::Size::ZERO;
	p.formatRenderers = function () {
		var newContentSizeHeight = 0, locRenderersContainer = this._elementRenderersContainer;
		var locElementRenders = this._elementRenders;
		var i, j, row, nextPosX, l;
		var lineHeight, offsetX;
		if (this._ignoreSize) {
			var newContentSizeWidth = 0;
			row = locElementRenders[0];
			nextPosX = 0;

			for (j = 0; j < row.length; j++) {
				l = row[j];
				l.setAnchorPoint(cc.p(0, 0));
				l.setPosition(nextPosX, 0);
				locRenderersContainer.addChild(l, 1, j);

				lineHeight = l.getLineHeight ? l.getLineHeight() : newContentSizeHeight;

				var iSize = l.getContentSize();
				newContentSizeWidth += iSize.width;
				newContentSizeHeight = Math.max(Math.min(newContentSizeHeight, lineHeight), iSize.height);
				nextPosX += iSize.width;
			}

			//Text flow horizontal alignment:
			if (this._textHorizontalAlignment !== cc.TEXT_ALIGNMENT_LEFT) {
				offsetX = 0;
				if (this._textHorizontalAlignment === cc.TEXT_ALIGNMENT_RIGHT)
					offsetX = this._contentSize.width - nextPosX;
				else if (this._textHorizontalAlignment === cc.TEXT_ALIGNMENT_CENTER)
					offsetX = (this._contentSize.width - nextPosX) / 2;

				for (j = 0; j < row.length; j++)
					row[j].x += offsetX;
			}

			locRenderersContainer.setContentSize(newContentSizeWidth, newContentSizeHeight);
			this.realSize_ = this.getContentSize();//add by lxl
		} else {
			var maxHeights = [];
			for (i = 0; i < locElementRenders.length; i++) {
				row = locElementRenders[i];
				var maxHeight = 0;
				for (j = 0; j < row.length; j++) {
					l = row[j];
					lineHeight = l.getLineHeight ? l.getLineHeight() : l.getContentSize().height;
					maxHeight = Math.max(Math.min(l.getContentSize().height, lineHeight), maxHeight);
				}
				maxHeights[i] = maxHeight;
				newContentSizeHeight += maxHeights[i];
			}

			var nextPosY = this._customSize.height;
			let realMaxWidth = 0;//add by lxl
			for (i = 0; i < locElementRenders.length; i++) {
				row = locElementRenders[i];
				nextPosX = 0;
				nextPosY -= (maxHeights[i] + this._verticalSpace);

				for (j = 0; j < row.length; j++) {
					l = row[j];
					l.setAnchorPoint(cc.p(0, 0));
					l.setPosition(cc.p(nextPosX, nextPosY));
					locRenderersContainer.addChild(l, 1);
					nextPosX += l.getContentSize().width;
				}
				//Text flow alignment(s)
				if (this._textHorizontalAlignment !== cc.TEXT_ALIGNMENT_LEFT || this._textVerticalAlignment !== cc.VERTICAL_TEXT_ALIGNMENT_TOP) {
					offsetX = 0;
					if (this._textHorizontalAlignment === cc.TEXT_ALIGNMENT_RIGHT)
						offsetX = this._contentSize.width - nextPosX;
					else if (this._textHorizontalAlignment === cc.TEXT_ALIGNMENT_CENTER)
						offsetX = (this._contentSize.width - nextPosX) / 2;

					var offsetY = 0;
					if (this._textVerticalAlignment === cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM)
						offsetY = this._customSize.height - newContentSizeHeight;
					else if (this._textVerticalAlignment === cc.VERTICAL_TEXT_ALIGNMENT_CENTER)
						offsetY = (this._customSize.height - newContentSizeHeight) / 2;

					for (j = 0; j < row.length; j++) {
						l = row[j];
						l.x += offsetX;
						l.y -= offsetY;
					}
				}
				if (nextPosX > realMaxWidth) {
					realMaxWidth = nextPosX;
				}
			}

			locRenderersContainer.setContentSize(this._contentSize);
			this.realSize_ = cocos2d::Size(realMaxWidth, newContentSizeHeight);//add by lxl
		}

		var length = locElementRenders.length;
		for (i = 0; i < length; i++) {
			locElementRenders[i].length = 0;
		}
		this._elementRenders.length = 0;

		this.setContentSize(this._ignoreSize ? this.getVirtualRendererSize() : this._customSize);
		this._updateContentSizeWithTextureSize(this._contentSize);

		locRenderersContainer.setPosition(this._contentSize.width * 0.5, this._contentSize.height * 0.5);
	}
}
